//
//  STRotationViewController.swift
//  BasicAnimation-Swift
//
//  Created by Stan on 2017-02-10.
//  Copyright © 2017 stan. All rights reserved.
//

import UIKit

class STRotationViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        添加缩放的动画
        redLayer.add(createAnimation(keyPath: "transform.scale.x", toValue: 0.5), forKey: nil)
        
        blueLayer.add(createAnimation(keyPath: "transform.scale.y", toValue: 0.5), forKey: nil)
        
        grayLayer.add(createAnimation(keyPath: "transform.scale.z", toValue: 0.5), forKey: nil)
        
        greenLayer.add(createAnimation(keyPath: "transform.scale", toValue: 0.5), forKey: nil)
        
        
        
//        添加旋转的动画
        redRotationLayer.add(createAnimation(keyPath: "transform.rotation.x", toValue: CGFloat(M_PI_4)), forKey: nil)
        
        blueRotationLayer.add(createAnimation(keyPath: "transform.rotation.y", toValue: CGFloat(M_PI_4)), forKey: nil)
        
        grayRotationLayer.add(createAnimation(keyPath: "transform.rotation.z", toValue: CGFloat(M_PI_4)), forKey: nil)

        
        
        //    添加几个Label，为了让演示的时候看上去更加清晰
        self.creatLabel(title: "缩放", frame: CGRect(x: 100, y: 80, width: 50, height: 20))
        self.creatLabel(title: "旋转", frame: CGRect(x: 250, y: 80, width: 50, height: 20))
        self.creatLabel(title: "X轴", frame: CGRect(x: 20, y: 125, width: 50, height: 20))
        self.creatLabel(title: "Y轴", frame: CGRect(x: 20, y: 225, width: 50, height: 20))
        self.creatLabel(title: "Z轴", frame: CGRect(x: 20, y: 325, width: 50, height: 20))
        self.creatLabel(title: "XYZ轴", frame: CGRect(x: 20, y: 425, width: 50, height: 20))


    }
    
    
    
    //MARK: - 懒加载
    
// 懒加载缩放的layer
    private lazy var redLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 125, y: 150), backgroundColor: UIColor.red)
        
        return layer
    }()
    
    private lazy var blueLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 125, y: 250), backgroundColor: UIColor.blue)
        
        return layer
    }()
    
    private lazy var grayLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 125, y: 350), backgroundColor: UIColor.gray)
        
        return layer
    }()
    
    private lazy var greenLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 125, y: 450), backgroundColor: UIColor.green)
        
        return layer
    }()
    
// 懒加载旋转的layer
    private lazy var redRotationLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 250, y: 150), backgroundColor: UIColor.red)
        
        return layer
    }()
    
    private lazy var blueRotationLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 250, y: 250), backgroundColor: UIColor.blue)
        
        return layer
    }()
    
    private lazy var grayRotationLayer: CALayer = {
        let layer = self.createLayer(position: CGPoint(x: 250, y: 350), backgroundColor: UIColor.gray)
        
        return layer
    }()
    

}




//MARK: - 抽取创建动画及Layer的公共方法
extension STRotationViewController {
    
    //    创建calayer
    fileprivate func createLayer (position: CGPoint, backgroundColor: UIColor) -> CALayer {
        //创建calayer
        let layer = CALayer()
        //设置位置和大小
        layer.position = position
        layer.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
        //设置背景颜色
        layer.backgroundColor = backgroundColor.cgColor
        //把layer添加到UIView的layer上
        self.view.layer.addSublayer(layer)
        
        return layer
    }
    
    
    
    //创建基础Animation
    fileprivate func createAnimation (keyPath: String, toValue: CGFloat) -> CABasicAnimation {
        //创建动画对象
        let scaleAni = CABasicAnimation()
        //设置动画属性
        scaleAni.keyPath = keyPath
        
        //设置动画的起始位置。也就是动画从哪里到哪里。不指定起点，默认就从positoin开始
        scaleAni.toValue = toValue
        
        //动画持续时间
        scaleAni.duration = 2;
        
        //动画重复次数
        scaleAni.repeatCount = Float(CGFloat.greatestFiniteMagnitude)
        
        return scaleAni;
    }
    

    //创建Label
    fileprivate func creatLabel (title: String, frame: CGRect) -> (){
      let label = UILabel()
        
        label.text = title
        label.frame = frame
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 12)
        
        self.view.addSubview(label)
        
    }

    
    
    
}
